using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Entities;
using Bll;

public partial class productdetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Check query string to avoid input address manually by user
            if (Request.QueryString.Count == 1)
            {
                if (string.IsNullOrEmpty(Request.QueryString["readID"]))
                {
                    odsProduct.SelectParameters[0].DefaultValue = "1";
                }
            }
            else
            {
                odsProduct.SelectParameters[0].DefaultValue = "1";
            }
        }

    }
    protected void gvProductDetail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName == "AddToCart")
        {            
            string proID = e.CommandArgument.ToString();
            if (!string.IsNullOrEmpty(proID))
            {
                CartBll cart = (CartBll)HttpContext.Current.Session["ShoppingCart"];
                if (cart == null)
                {
                    cart = new CartBll();
                    cart.Add(proID);
                    HttpContext.Current.Session["ShoppingCart"] = cart;
                    Response.Redirect("shoppingcart.aspx");
                }
                else
                {
                    cart.Add(proID);
                    HttpContext.Current.Session["ShoppingCart"] = cart;
                    Response.Redirect("shoppingcart.aspx");
                }

            }

        }
    }

    protected void btnCommitReview_Click(object sender, EventArgs e)
    {
        string title = txtReviewTitle.Text;
        string content = txtNewReview.Text;
        if (HttpContext.Current.Session["Login"] != null)
        {
            if (!string.IsNullOrEmpty(title))
            {
                if (!string.IsNullOrEmpty(content))
                {
                    string id = Request.QueryString[0].ToString();
                    int rating = int.Parse(ddlVote.SelectedValue);

                    ReviewBll reviewBll = new ReviewBll();
                    ReviewInfo reviewInfo = new ReviewInfo();
                    reviewInfo.ProductID = int.Parse(id);
                    reviewInfo.ReviewRating = rating;
                    reviewInfo.ReviewTitle = title;
                    reviewInfo.Contents = content;
                    ArrayList arr = (ArrayList) Session["Login"];
                    string username = arr[0].ToString();
                    reviewInfo.UserName = username;
                    reviewBll.Insert(reviewInfo);
                    //also update Product Rating
                    ProductBll proBll = new ProductBll();
                    proBll.UpdateRating(Request.QueryString["readID"]);
                    gvProductDetail.DataBind();
                    ClearText();
                }
                else
                {
                    lblReviewError.Text = "Please enter Review Content";
                }
            }
            else
            {
                lblReviewError.Text = "Please enter Review Title";
            }
        }
        else
            lblReviewError.Text = "You have to login to leave comment !";

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearText();
    }

    private void ClearText()
    {
        txtReviewTitle.Text = "";
        txtNewReview.Text = "";
        lblReviewError.Text = "";
        
    }
}
